﻿using Quellenverwaltung.Word;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Quellenverwaltung.Meta
{
    class BuildJournalSource
    {
        Configuration configuration;

        public BuildJournalSource()
        {
            configuration = Configuration.getConfiguration();
        }

        public Data.Source.ArticleInAPeriodical FromMetaData(Dictionary<string, string> meta)
        {
            Data.Source.ArticleInAPeriodical journal = new Data.Source.ArticleInAPeriodical();

            // Process Accessed Date
            DateTime currentDate = DateTime.Now;
            if (currentDate != null)
            {
                journal.DayAccessed = currentDate.Day.ToString();
                journal.MonthAccessed = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(currentDate.Month);
                journal.YearAccessed = currentDate.Year.ToString();
            }

            journal.Publisher = getString(meta, new string[]{"citation_publisher"});
            journal.Title = getString(meta, new string[]{"citation_title"});

            string firstpage = getString(meta, new string[]{"citation_firstpage"});
            string lastpage = getString(meta, new string[]{"citation_lastpage"});
            if (!String.IsNullOrEmpty(firstpage) && !String.IsNullOrEmpty(lastpage)) {
                journal.Pages = firstpage + " - " + lastpage;
            }
            journal.DOI = getString(meta, new string[]{"citation_doi"});
            journal.PeriodicalTitle = getString(meta, new string[]{"citation_journal_title"});
            journal.Volume = getString(meta, new string[]{"citation_volume"});
            journal.Issue = getString(meta, new string[]{"citation_issue"});
            journal.Edition = getString(meta, new string[]{"citation_edition"});
            journal.StandardNumber = getString(meta, new string[]{"citation_issn"});
            if (String.IsNullOrEmpty(journal.DOI))
            {
                journal.URL = getString(meta, new string[] { "journal_url" });
            }
            else
            {
                journal.URL = "http://dx.doi.org/" + journal.DOI;
            }
            string authornames = getString(meta,new string[]{"citation_author"});
            if (authornames != null && authornames.Length > 0)
            {
                journal.AuthorNamesAsString = authornames;
                if (journal.AuthorNamesAsString.Count() < authornames.Length * 0.8)
                {
                    // Conversion propably failed. -> use Corporate instead
                    journal.AuthorCorporateAsString = authornames;
                }
            }

            DateTime? date = getDate(meta, new string[] { "citation_publication_date" });
            if (date != null)
            {
                journal.Day = ((DateTime)date).Day.ToString();
                journal.Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(((DateTime)date).Month);
                journal.Year = ((DateTime)date).Year.ToString();
            }

            return journal;
        }

        private DateTime? getDate(Dictionary<string, string> meta, String[] tags)
        {
            DateTime? dateTime = null;
            string dateVal;

            foreach (String key in tags) 
            {
                if (meta.TryGetValue(key, out dateVal))
                {
                    if (dateVal.Length > 0)
                    {
                        try
                        {
                            dateTime = DateTime.Parse(dateVal);
                            if (dateTime != null)
                            {
                                break;
                            }
                        }
                        catch { };
                    }
                }
            }

            return dateTime;
        }

        public string getString(Dictionary<string, string> meta, String[] tags)
        {
            string val;

            foreach (String key in tags)
            {
                if (meta.TryGetValue(key, out val))
                {
                    if (val != null && val.Length > 0)
                    {
                        return val;
                    }
                }
            }

            return "";
        }

    }
}
